package com.example.demo.controller;

import com.example.demo.common.base.JsonResult;
import com.example.demo.entity.Message;
import com.example.demo.handler.MyWebSocketHandler;
import com.example.demo.server.WebSocketServer;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;


@RestController
public class DemoController {

    @GetMapping("index")
    public ResponseEntity<String> index(){
        return ResponseEntity.ok("请求成功");
    }

    @GetMapping("page")
    public ModelAndView page(){
        return new ModelAndView("websocket");
    }

    @RequestMapping("/push/{toUserId}")
    public ResponseEntity<String> pushToWeb(String message, @PathVariable String toUserId) throws IOException {
        WebSocketServer.sendInfo(message,toUserId);
        return ResponseEntity.ok("MSG SEND SUCCESS");
    }

    @PostMapping("/favor/{toUserId}")
    public JsonResult pushMessageToWeb(@PathVariable String toUserId){
        Message message = new Message();
        message.setNotifyContent("嗨喽");
        new MyWebSocketHandler().notify(toUserId,message);
        return JsonResult.success();
    }
}